@echo ons
setlocal enableextensions

set Drive=%~d0
set CurBatPath=%~dp0
set PACK_LOG="%CurBatPath%CI_PACK.log"
set BASE_PATH=%CurBatPath%..\..\
echo BASE_PATH is %BASE_PATH% >> %PACK_LOG%
set FRAMEWORK_PATH=%BASE_PATH%..\..\framework
set OUTPUT_PKG_PATH=%FRAMEWORK_PATH%\output_pkg
set FINAL_PKG_PATH=%BASE_PATH%\output_pkg
set open_source_obligation_path=%1
call :main
endlocal
goto :eof

:execute_build_script
    call %CurBatPath%\build_opensource.bat %open_source_obligation_path%
    if %errorlevel% NEQ 0 (
        echo Failed to execute the build script! >> %PACK_LOG%
        exit 1
    )
goto :eof

:create_dir
    ::create bin/applications path
    if not exist %OUTPUT_PKG_PATH%\bin\applications mkdir %OUTPUT_PKG_PATH%\bin\applications

    :: create pdb for debug
    if not exist %OUTPUT_PKG_PATH%\pdb mkdir %OUTPUT_PKG_PATH%\pdb

    ::create conf path
    if not exist %OUTPUT_PKG_PATH%\conf mkdir %OUTPUT_PKG_PATH%\conf

    ::create service path
    if not exist %OUTPUT_PKG_PATH%\install mkdir %OUTPUT_PKG_PATH%\install

    if not exist %FINAL_PKG_PATH% mkdir %FINAL_PKG_PATH%
goto :eof


:copy_file
    ::copy plugin_attribute json file
    if not exist %BASE_PATH%conf\plugin_attribute_*.json (
        echo The plugin attribute json file cannot be found! >> %PACK_LOG%
        exit 1
    )
    xcopy %BASE_PATH%conf\plugin_attribute_*.json %OUTPUT_PKG_PATH%\conf /e/y

    ::copy dll file
    if not exist %BASE_PATH%..\..\Windows_Compile\vsprj\bin\bin\*.dll (
        echo The FilePlugin exe file cannot be found! >> %PACK_LOG%
        exit 1
    )
    xcopy %BASE_PATH%..\..\Windows_Compile\vsprj\bin\bin\*.dll %OUTPUT_PKG_PATH%\bin /e/y

    xcopy %BASE_PATH%..\..\Windows_Compile\vsprj\bin\bin\*.pdb %OUTPUT_PKG_PATH%\pdb /e/y
    ::copy powershell
    xcopy %BASE_PATH%src\protect_engines\hyperv\api\powershell\*.ps1 %OUTPUT_PKG_PATH%\bin /e/y
    ::copy other conf file
    xcopy %BASE_PATH%conf\*.conf %OUTPUT_PKG_PATH%\conf /e/y

    ::copy hcpconf.ini
    if not exist %BASE_PATH%\conf\hcpconf.ini (
        echo The hcpconf.ini file cannot be found! >> %PACK_LOG%
        exit 1
    )
    xcopy %BASE_PATH%\conf\hcpconf.ini %OUTPUT_PKG_PATH%\conf /e/y

    ::copy install.bat
    if not exist %BASE_PATH%install\install.bat (
        echo The install file cannot be found! >> %PACK_LOG%
        exit 1
    )
    xcopy %BASE_PATH%install\install.bat %OUTPUT_PKG_PATH%\install /e/y
    
    ::copy start.bat
    if not exist %BASE_PATH%install\start.bat (
        echo The start file cannot be found! >> %PACK_LOG%
        exit 1
    )
    xcopy %BASE_PATH%install\start.bat %OUTPUT_PKG_PATH%\install /e/y

goto :eof


:execute_pack_script
    call %FRAMEWORK_PATH%\build\pack.bat
    if %errorlevel% NEQ 0 (
        echo Failed to execute framework pack script! >> %PACK_LOG%
        exit 1
    )
    xcopy %OUTPUT_PKG_PATH% %FINAL_PKG_PATH% /e/y
goto :eof

:upload_pack
    xcopy "%FRAMEWORK_PATH%\output_pkg\VirtualizationPlugin.zip" "%open_source_obligation_path%\Plugins\Windows\" /y
goto :eof

:main
    echo "#######################################################################################################"
    echo Start pack VirtualizationPlugin.
    echo Start pack VirtualizationPlugin. >> %PACK_LOG%
    echo "#######################################################################################################"

    rd /s /q  %OUTPUT_PKG_PATH%
    rd /s /q  %FINAL_PKG_PATH%
    call :execute_build_script
    
    call :create_dir

    call :copy_file

    call :execute_pack_script

    call :upload_pack

    echo "#######################################################################################################"
    echo VirtualizationPlugin pack success.
    echo VirtualizationPlugin pack success. >> %PACK_LOG%
    echo "#######################################################################################################"
goto :eof